package com.etc.controller;

import com.etc.pojo.EmpMst;
import com.etc.pojo.Result;
import com.etc.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {


    @Autowired
    EmpService empService;

    // emp/getAll
    @GetMapping("/getAll")
    public Result getAll() {

        List<EmpMst> list = empService.getAll();
        return Result.ok(list);
    }


    @PostMapping("/add")
    public Result add(@RequestBody EmpMst emp) {

        empService.addEmp(emp);
        return Result.ok("插入成功");
    }

    @DeleteMapping("delete")
    public Result delete(@RequestParam("id")int id) {
        empService.deleteById(id);
        return Result.ok("删除成功！");
    }

    // ?empName=tom2&sex=m
    @GetMapping("search")
    public Result search(EmpMst emp) {
        PageHelper.startPage(1, 5);

        List<EmpMst> elist = empService.findByCondition(emp);

        PageInfo<EmpMst> pageInfo = new PageInfo<>(elist);
        pageInfo.setTotal(pageInfo.getTotal());
        pageInfo.setPages(pageInfo.getPages());
        pageInfo.setPageNum(pageInfo.getPageNum());

        return Result.ok(pageInfo);
    }


}
